<?php
namespace app\api\controller\article;
use app\api\controller\Init;
use fast\ArrayHelper;

class Admiration extends Init
{
    public function initialize() {
        parent::initialize();
        $this->service = model('article/Admiration', 'service');
    }

    /**
     * 点赞列表
     * @return array
     *
     * @author <xiachao>
     * @date   2018/11/27 10:12
     */
    public function lists()
    {
        $order = $this->request->param('order/s','update_time desc');
        $field = $this->request->param('field/s',true);
        $relations = array_filter($this->request->param('relations/a',[]));
        $maps = [];
        $maps['site_id'] = $this->site['id'];
        $maps['current_id'] = $this->site['custom_id'];
        $maps['article_id'] = $this->request->param('id/d',0);
        if ($maps['article_id'] < 1) {
            return $this->response('文章信息不能为空', [], -300001);
        }
        $maps['status'] = 1;
        $result = $this->service->lists($maps,$order,$this->page,$this->limit,$field,$relations);
        return $this->response("success", $result);
    }

    /**
     * 文章点赞总数
     * @return array
     *
     * @author <xiachao>
     * @date   2018/11/27 11:12
     */
    public function admiration_sum()
    {
        $maps = [];
        $maps['site_id'] = $this->site['id'];
        $maps['current_id'] = $this->site['custom_id'];
        $maps['article_id'] = $this->request->param('id/d',0);
        if ($maps['article_id'] < 1) {
            return $this->response('文章信息不能为空', [], -300001);
        }
        $maps['status'] = 1;
        $maps['type'] = 'sum';
        $result = $this->service->lists($maps,'',false,false);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300002);
        }
        return $this->response("success", $result);
    }



    /**
     * 添加点赞
     * @param array $admiration 点赞信息
     * @return mixed
     *
     * @author <xiachao>
     * @date   2018/11/27 10:12
     */
    public function create() {
        if (!isset($this->params)) {
            return $this->response('点赞信息不能为空',[],-300003);
        }
        $validate = new \app\article\validate\Admiration;
        if(!$validate->scene('create')->check($this->params)){
            return $this->response($validate->getError(),[],-300004);
        }
        $date = [];
        $date['current_id'] = $this->site['custom_id'];
        $date['site_id'] = $this->site['id'];
        $date['ip'] = (string) $_SERVER['REMOTE_ADDR'];
        $date['article_id'] = (int) $this->params['article_id'];
        $date['username'] = $this->params['username'];
        $date['avatar'] = $this->params['avatar'];
        $date['status'] = 1;
        $result = $this->service->create($date);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }

    /**
     * 修改点赞状态
     * @param array $admiration 点赞信息
     * @param int $id 点赞记录id
     * @return mixed
     *
     * @author <xiachao>
     * @date   2018/11/27 10:12
     */
    public function save() {
        $validate = new \app\article\validate\Admiration;
        if(!$validate->scene('edit')->check($this->params)){
            return $this->response($validate->getError(),[],-300004);
        }
        $date = [];
        $date['current_id'] = $this->site['custom_id'];
        $date['site_id'] = $this->site['id'];
        $date['ip'] = (string) $_SERVER['REMOTE_ADDR'];
        $date['article_id'] = (int) $this->params['article_id'];
        $date['username'] = $this->params['username'];
        $date['avatar'] = $this->params['avatar'];
        $date['status'] = (int) $this->params['status'];;
        $result = $this->service->save($date);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300101);
        }
        return $this->response("success", $result);
    }

    /**
     * 查询是否点赞
     * @return array
     *
     * @author <xiachao>
     * @date   2018/11/27 10:12
     */
    public function detail()
    {
        $field = $this->request->param('field/s',true);
        $relations = array_filter($this->request->param('relations/a',[]));
        $maps = [];
        $maps['site_id'] = $this->site['id'];
        $maps['current_id'] = $this->site['custom_id'];
        $maps['ip'] = (string) $_SERVER['REMOTE_ADDR'];
        $maps['article_id'] = $this->request->param('article_id/d',0);
        $maps['username'] = $this->request->param('username/s','');
        if ($maps['article_id'] < 1) {
            return $this->response('文章信息不能为空', [], -300001);
        }
        if (empty($maps['username'])) {
            return $this->response('用户名不能为空', [], -300001);
        }
        $result = $this->service->detail($maps,$field,$relations);
        if (!$result) {
            return $this->response($this->service->getError(),[],-300002);
        }
        return $this->response("success", $result);
    }

}